package com.james.happyfarm.servlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.TimeZone;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.james.happyfarm.JDO.Message;
import com.james.happyfarm.JDO.PMF;
import com.james.happyfarm.PO.Animal;
import com.james.happyfarm.PO.Friend;
import com.james.happyfarm.modules.RanchModule;
import com.james.happyfarm.network.KaiXinWap;

public class RanchSpy extends HttpServlet {

	private static final long serialVersionUID = -3158405300463206255L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int hour = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")).get(Calendar.HOUR_OF_DAY);
		if (hour >= 2 && hour < 6)
			return;
		
		String verifyCode = KaiXinWap.ME_VERIFYCODE;
		RanchModule rm = new RanchModule();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		for (Friend friend : rm.getStealableFriend(verifyCode)) {
			for (Animal animal : rm.getStealableAnimal(friend.getRanch(), verifyCode).getAnimalList()) {
				pm.makePersistent(new Message(friend.getUserName(), rm.stealAnimal(animal, verifyCode)));
			}
		}
	}

}
